package cn.sdxx.qddata.controller;

import cn.sdxx.qddata.common.R;
import cn.sdxx.qddata.entity.Brand;
import cn.sdxx.qddata.entity.User;
import cn.sdxx.qddata.service.BrandService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Api(tags = "Brand模块")
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @ApiOperation(value = "检查品牌名称是否已存在")
    @ApiImplicitParam(name = "brandName", value = "需要检查的品牌名称", required = true)
    @GetMapping("/check")
    public R checkBrandName(@RequestParam(value = "brandName") String brandName) {
        log.info("检查品牌名称 --> {}", brandName);
        LambdaQueryWrapper<Brand> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Brand::getBrandName, brandName);
        Brand brand = brandService.getOne(lqw);
        if (brand != null) {
            //该品牌名称已经占用了
            return R.fail("此品牌名称已被注册");
        }
        return R.success("此品牌名称未被注册，你可以使用");

    }

    @ApiOperation(value = "注册品牌")
    @PostMapping
    @RequiresAuthentication
    public R add(@RequestBody Brand brand) {
        log.info("注册品牌 --> {}", brand);
        //检查 brand
        LambdaQueryWrapper<Brand> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Brand::getBrandName, brand.getBrandName());
        Brand brandEx = brandService.getOne(lqw);
        if (brandEx != null) {
            //该品牌名称已经占用了
            return R.fail("注册失败！品牌" + brand.getBrandName() + "已被注册");
        }
        //检查 code （统一社会信用代码）必须18位

        //检查是否符合规则
        String code = brand.getCode();
        if (code == null || code.length() != 18) {
            return R.fail("注册失败！企业统一社会信用代码长度必须是18位");
        }
        if(!code.matches("[0-9a-zA-Z]{18}")){
            return R.fail("注册失败！企业统一社会信用代码为18位数字或字母组合");
        }

        String brandName = brand.getBrandName();
        if (brandName == null || brandName.length() < 2) {
            return R.fail("注册失败！品牌名称长度至少2个字符");
        }

        String companyName = brand.getCompanyName();
        if (companyName == null || companyName.length() < 4) {
            return R.fail("注册失败！企业（公司）名称长度至少4个字符");
        }

        //开始注册
        brand.setId(null);
        boolean b = brandService.save(brand);
        if (b) {
            return R.success("品牌注册成功！");
        }
        return R.fail("品牌注册失败！");
    }


    @ApiOperation(value = "按品牌名称、公司名称查询，分页列出品牌信息")
    @GetMapping("/list")
    @RequiresAuthentication
    public R list(@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
                  @RequestParam(required = false, value = "pageSize", defaultValue = "5") int pageSize,
                  @RequestParam(required = false, value = "brandName") String brandName,
                  @RequestParam(required = false, value = "companyName") String companyName) {

        //分页构造器
        Page<Brand> pageInfo = new Page<>(pageNo, pageSize);
        //条件构造器
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(brandName != null, Brand::getBrandName, brandName);
        queryWrapper.like(companyName != null, Brand::getCompanyName, companyName);
        //添加排序条件，根据id进行排序
        queryWrapper.orderByAsc(Brand::getId);
        brandService.page(pageInfo, queryWrapper);
        log.info("分页查询品牌列表 pageInfo --> {}", pageInfo);
        return R.success(pageInfo);

    }

    @ApiOperation(value = "修改品牌（必须带id）")
    @DeleteMapping()
    @RequiresAuthentication
    public R updateBrand(@RequestBody Brand brand) {
        log.info("修改品牌：{}", brand);

        boolean b = brandService.updateById(brand);
        if(b){
            return R.success("品牌修改成功！");
        }
        return R.success("品牌修改失败！");

    }

    @ApiOperation(value = "删除品牌")
    @ApiImplicitParam(name = "id", value = "品牌id", required = true)
    @PutMapping()
    @RequiresAuthentication
    public R deleteBrand(@RequestParam("id") Long id) {
        log.info("删除品牌，id为：{}", id);

        boolean b = brandService.removeById(id);
        if(b){
            return R.success("品牌删除成功");
        }
        return R.success("品牌删除失败");

    }


}
